﻿Imports System.Text.RegularExpressions
Imports System.Text
Imports ajayumi.develop.VBasic.Core

''' <summary>
''' 网络资源服务
''' </summary>
''' <remarks></remarks>
Public Class WebResourceService

    ''' <summary>
    ''' 从网页源码中获取资源列表
    ''' </summary>
    ''' <param name="htmlCode">网页源码</param>
    ''' <param name="resourceType">资源类型</param>
    ''' <returns></returns>
    Public Shared Function GetResourcesList(htmlCode As String, ByVal resourceType As ResourceType) As List(Of String)
        Dim _List As New List(Of String)()
        Dim _MatchCollection As MatchCollection = Nothing
        Dim _Result As String = String.Empty
        Dim _ResourceInfo As WebResourceInfo = Nothing

        If String.IsNullOrEmpty(htmlCode) Then Return _List

        Try
            _MatchCollection = GetResourceCollection(htmlCode, resourceType)

            For Each _Match As Match In _MatchCollection
                Select Case resourceType
                    Case Service.ResourceType.None
                        Exit Select
                    Case Service.ResourceType.Url
                        _Result = Regex.Match(_Match.Value, RegexPatten.Lock_HttpUrlExpression, _
                                              RegexOptions.IgnoreCase Or RegexOptions.Multiline).Groups("url").Value
                        Exit Select
                    Case Service.ResourceType.Image
                        _Result = Regex.Match(_Match.Value, RegexPatten.Lock_ImgUrlExpression, _
                                              RegexOptions.IgnoreCase Or RegexOptions.Multiline).Groups("img").Value
                        Exit Select
                End Select

                _List.Add(_Result)
            Next
        Catch ex As Exception
            Throw ex
        Finally
            _MatchCollection = Nothing
        End Try

        Return _List
    End Function

    ''' <summary>
    ''' 从网页源码中获取超链接列表
    ''' </summary>
    ''' <param name="url"></param>
    ''' <param name="htmlCode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetResourcesList(ByVal url As String, ByVal htmlCode As String, ByVal resourceType As ResourceType) As List(Of WebResourceInfo)
        Dim _List As New List(Of WebResourceInfo)()
        Dim _MatchCollection As MatchCollection = Nothing
        Dim _Domail As String = String.Empty
        Dim _NoPageUrl As String = String.Empty
        Dim _Result As String = String.Empty
        Dim _Array() As String = Nothing
        Dim _ResourceInfo As WebResourceInfo = Nothing
        Dim _Title As String = StringHelper.GetHtmlDocTitle(htmlCode)

        If String.IsNullOrEmpty(htmlCode) Then Return _List

        Try
            _Domail = Regex.Match(url, RegexPatten.Lock_HttpDomainExpression, RegexOptions.IgnoreCase Or RegexOptions.Singleline).Value
            _NoPageUrl = Regex.Match(url, RegexPatten.Lock_NoPageUrlExpression, RegexOptions.IgnoreCase Or RegexOptions.Singleline).Value

            _MatchCollection = GetResourceCollection(htmlCode, resourceType)

            For Each _Match As Match In _MatchCollection
                _Result = _Match.Groups("link").Value

                '去掉超链接后面的 /
                _Result = TrimSlash(_Result)

                '这里是没有 http://...... 头的地址
                If Regex.Match(_Result, "^(http://)+", RegexOptions.IgnoreCase Or RegexOptions.Multiline).Success = False Then
                    '1.是否有 / 开头，有则直接加上http://......
                    '2.是否有 ？开头，有则直接用当前的网络地址加上TempStr
                    '3.是否有 ../ 开头，有则需要进行相应的处理
                    If Regex.Match(_Result, "^/+").Success = True Then
                        _Result = String.Concat(_Domail, _Result)
                    ElseIf Regex.Match(_Result, "^(\?)+").Success = True Then
                        If Regex.Match(url, Lock_HttpExpression, RegexOptions.IgnoreCase Or RegexOptions.Multiline).Success = True Then
                            _Result = String.Concat(Regex.Match(url, Lock_HttpExpression, RegexOptions.IgnoreCase Or RegexOptions.Multiline).Value, _Result)
                        Else
                            _Result = String.Concat(url, _Result)
                        End If
                    ElseIf Regex.Match(_Result, "^(\.\./)+").Success = True Then

                        '这里将http://....../字符串中的 http:// / 去掉，然后使用split拆分字符串
                        _Array = Split(Regex.Replace(_NoPageUrl, "^(http://)+", String.Empty, RegexOptions.IgnoreCase Or RegexOptions.Multiline), _
                                       "/")
                        Dim _StringBuilder As New StringBuilder()
                        Dim Count As Integer = 0
                        If _Array.Length <= Split(_Result, "../").Length Then
                            Count = 0
                        Else
                            Count = _Array.Length - Split(_Result, "../").Length - 1
                        End If
                        For i As Integer = 0 To Count
                            _StringBuilder.Append(String.Concat(_Array(i), "/"))
                        Next
                        _Result = String.Concat("http://", _StringBuilder.ToString(), Regex.Replace(_Result, "(\.\./)+", String.Empty))

                        _StringBuilder = Nothing
                    Else
                        If String.IsNullOrEmpty(_NoPageUrl) Then
                            _Result = String.Concat(url, "/", _Result)
                        Else
                            _Result = String.Concat(_NoPageUrl, _Result)
                        End If
                    End If
                End If

                _ResourceInfo = New WebResourceInfo()
                With _ResourceInfo
                    .Title = _Title
                    .Url = _Result
                    .SourceUrl = url
                    .ResourceType = resourceType
                    .CreationTime = DateTime.Now
                End With
                _List.Add(_ResourceInfo)
            Next
        Catch ex As Exception
            Throw ex
        Finally
            _MatchCollection = Nothing
        End Try

        Return _List
    End Function

    ''' <summary>
    ''' 去除斜杠
    ''' </summary>
    ''' <param name="str"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function TrimSlash(ByVal str As String) As String
        Dim _Result As String = Regex.Match(str, ".*[^/]").Value
        Return _Result
    End Function

    ''' <summary>
    ''' 获取资源集合
    ''' </summary>
    ''' <param name="htmlCode">网页源码</param>
    ''' <param name="resourceType">资源类型</param>
    ''' <returns>MatchCollection</returns>
    Private Shared Function GetResourceCollection(ByVal htmlCode As String, ByVal resourceType As ResourceType) As MatchCollection
        Select Case resourceType
            Case Service.ResourceType.Url
                Return Regex.Matches(htmlCode, RegexPatten.Lock_HttpHrefExpression, RegexOptions.IgnoreCase Or RegexOptions.Multiline)
            Case Service.ResourceType.Image
                Return Regex.Matches(htmlCode, RegexPatten.Lock_ImgSrcExpression, RegexOptions.IgnoreCase Or RegexOptions.Multiline)
            Case Service.ResourceType.None
                Exit Select
        End Select

        Return Nothing
    End Function
End Class
